package com.dyh.quickdemozwei.d1216;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dyh.quickdemozwei.util.FileUtil;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: yhdong
 * @Date: 2021/12/16 9:38
 */
@Slf4j
public class D1216 {

    @Test
    public void test14(){
        String str = "1234";

        System.out.println(str.substring(0,3));
        String ar[] = str.split("/");
        System.out.println();

    }


    @Test
    public void test13(){
        String s1 ="dawdawd";
        String s2 ="dawd.dawd";
        String s3 ="locality_life.verification/locality_life1.verification1";

        System.out.println(getLeafKey(s1));
        System.out.println(getLeafKey(s2));
        System.out.println(getLeafKey(s3));

        List<JSONObject> joList = Lists.newArrayList(
          new JSONObject(),
          new JSONObject(),
          new JSONObject()
        );

        List<JSONObject> jo2 =
        joList.stream().peek(
                e->e.put("name","dawdawd")
        ).collect(Collectors.toList());

        System.out.println();
    }

    public String getLeafKey(String s){
        String[] arr = s.split("/");
        return arr[arr.length-1];
    }

    @Test
    public void test12(){

        ReentrantLock lock = new ReentrantLock();
        lock.lock();

        log.info(Thread.currentThread().getName());

        FutureTask<String> ft = new FutureTask<>(
                ()->{
                    lock.lock();
//                    Thread.sleep(5000L);
                    log.info(Thread.currentThread().getName());
                    return "ok";
                }
        );
        Thread t = new Thread(ft);

        long start = System.currentTimeMillis();
        try {
            t.start();
//            String res = ft.get(4, TimeUnit.SECONDS);
            String res = ft.get();

        }  catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        finally {
            t.interrupt();
        }


        /*4020*/

        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    @Test
    public void test11(){

        ReentrantLock lock = new ReentrantLock();
        lock.lock();

        log.info(Thread.currentThread().getName());

        FutureTask<String> ft = new FutureTask<>(
                ()->{
                    lock.lock();
//                    Thread.sleep(5000L);
                    log.info(Thread.currentThread().getName());
                    return "ok";
                }
        );
        Thread t = new Thread(ft);

        long start = System.currentTimeMillis();
        try {
            t.start();
//            String res = ft.get(4, TimeUnit.SECONDS);
            String res = ft.get(4, TimeUnit.SECONDS);

        } catch (TimeoutException e) {
            System.out.println("timeOut!!");
            e.printStackTrace();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
//        finally {
//            t.interrupt();
//        }


        /*4020*/

        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }


    @Test
    public void test10(){
        Thread td = new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("线程是否处于中断状态" + Thread.currentThread().isInterrupted());
            }
        });
        td.start();
        td.interrupt();

        System.out.println("线程是否处于中断状态" + td.isInterrupted());

    }

    @Test
    public void test9(){
        Thread td = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(100000000L);
                } catch (InterruptedException e) {
                    System.out.println("线程是否处于中断状态" + Thread.currentThread().isInterrupted());
                    e.printStackTrace();
                    System.out.println("abc");
                }
                System.out.println("def");
            }
        });
        td.start();
        td.interrupt();
    }




    @Test
    public void test8(){

        System.out.println(        5000 - System.currentTimeMillis()%1000);


        System.out.println( (System.currentTimeMillis()/1000)%60) ;
        /*
        * 2022-01-08 00:00:00 1641571200
        *
        *
        * 2022-01-08 00:00:01 1641571201
        * */

        long l0 = 1641571200L;
        long l1 = 1641571201L;
        long l2 = 1641571202L;
        long l4 = 1641571204L;

        long l00 = 1641571260L;

        System.out.println(l0%60);//0
        System.out.println(l1%60);//1
        System.out.println(l2%60);//2
        System.out.println(l4%60);//4
        System.out.println(l00%60);//0






    }


    @Test
    public void test7(){
        String a = "";
        String b = "b";
        String c = "";
        String d = "d";

        String res = Joiner.on(".").join(a,b,c,d);

        System.out.println(res);
    }

    @Test
    public void test6(){

        Map<String,List> map = new HashMap<>();
        map.put("awd",new ArrayList());

        fun(map.get("awd"));


        System.out.println();
    }

    public  void fun(List list){
        list.add(1);
        list.add(2);
    }


    @Test
    public void test5(){
        Map<String,String> m1 = new HashMap<>();
        m1.put("1","11");
        m1.put("2","22");

        Map<String,String> m2 =new HashMap<>();
        m2.put("2","22");
        m2.put("3","33");

        m1.keySet().removeAll(m2.keySet());


        System.out.println();
    }


    @Test
    public void test4(){
//        String s = null;
        String s = "";
        String [] arr = s.split(",");
        System.out.println();
    }

    @Test
    public void test3(){
        StringBuilder sb = new StringBuilder("1asdawd");

        sb.insert(0,"fff-");

        System.out.println(sb.toString());
    }

    @Test
    public void test2(){

        int [][] arr ={
            {1,2,3},
            {4,5,6},
            {7,8,9}
        };

//        for (int i = 0;i<3;i++){
//            for (int j = 0;j<3;j++){
//                if (i==1)
//                    break;
//                System.out.println(arr[i][j]);
//            }
//        }
        L1:
        for (int i = 0;i<3;i++){
            for (int j = 0;j<3;j++){
                if (i==1)
                    break L1;
                System.out.println(arr[i][j]);
            }
            System.out.println("out"+i);
        }

    }



    @Test
    public void test1(){

        List list = Lists.newArrayList(1,2,3,4,5,6,7);

        for (int i = -1;i>0;i--){
            System.out.println(list.get(i));
        }
//        List<List> pp = Lists.partition(list,5);

        System.out.println();
    }

    @Test
    public void test(){
       String raw = FileUtil.readFile("D:\\neo-work\\quick-demo-zwei\\src\\main\\java\\com\\dyh\\quickdemozwei\\d1216\\126488005O2O.json");
        List<JSONObject> ja = JSON.parseObject(raw,List.class);
        System.out.println();

        List<String> coll = new ArrayList<>();

        for (JSONObject jsonObject : ja) {
            passOneJo(jsonObject,coll);
        }

        System.out.println();
    }

    public void passOneJo(JSONObject jo, List<String> coll){
        coll.add(jo.getString("cid"));

        if (jo.get("item_cat")!=null){
            for (Object item_cat : jo.getJSONArray("item_cat")) {
                JSONObject sjo = (JSONObject)item_cat;
                passOneJo(sjo,coll);
            }
        }
    }
}
